﻿package net.wangit.view.element.component;

import net.wangit.Page;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.ElementWriter;
import net.wangit.util.Checker;
import net.wangit.util.ClassUtil;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.ElementConfigurationException;
import net.wangit.view.element.util.ElementUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class CustomElement extends ElementComponent {

	private static final Log log = LogFactory
			.getLog(net.wangit.view.element.component.CustomElement.class);

	public CustomElement() {
	}

	protected void configure(Row row1) {
	}

	protected ElementWriter getWriter() {
		String custom;
		custom = getCustom();
		if (Checker.isEmpty(custom)) {
			log
					.error((new StringBuilder(
							"this custom element writer is null: ")).append(
							ElementUtil.getUrl(this)).toString());
			throw new ElementConfigurationException(
					"could not load element writer: ", getParent().getDomain(),
					getParent().getId(), getId(), "writer", null);
		}
		try {
			return (ElementWriter) ClassUtil.loadClass(custom).newInstance();
		} catch (Exception e) {
			log
					.error((new StringBuilder(
							"could not load the element writer: ")).append(
							custom).append(",")
							.append(ElementUtil.getUrl(this)).toString());
			throw new ElementConfigurationException((new StringBuilder(
					"could not load the element writer: ")).append(custom)
					.toString(), getParent().getDomain(), getParent().getId(),
					getId(), "writer", e);
		}
	}

	protected void cloneAfter(ActionContext actioncontext, Page page1) {
	}

}
